//
//  C_RankingListViewController.m
//  音悦咖
//
//  Created by Best on 15/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
/*
 http://api.dongting.com/channel/ranklist/2406/songs?page=1&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music
 */
//

#import "C_RankingListViewController.h"
#import "C_RankingCollectionViewCell.h"
#import "C_RankingListPlayerViewController.h"

@interface C_RankingListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation C_RankingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(150, 100, 30, 1);
    self.titleLabel.text = @"排行";
    self.rightButton.hidden = YES;
    
    backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    backgroundImage.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"blackgound", @"jpg")];
    [self.view addSubview:backgroundImage];
    backgroundImage.userInteractionEnabled = YES;
    [backgroundImage release];
    [self getDataArr];
}



- (void)getDataArr
{
      [AFNetworkHandler GETWithAFNByURL:self.rankingURL completion:^(id result) {
          
          self.dataArr = [RankingListModel arrayWithModelByArray:[result objectForKey:@"data"]];
          [self createCollection];
          
      }];
}




#pragma mark 创建collection
- (void)createCollection
{
       UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, 90);
    collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor clearColor];
    collection.delegate = self;
    collection.dataSource = self;
    
    [backgroundImage addSubview:collection];
    [collection release];
    
    [collection registerClass:[C_RankingCollectionViewCell class] forCellWithReuseIdentifier:@"C_RankingCollectionViewCell"];
    
}

#pragma mark Collection Delegate/DataSourse

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    C_RankingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_RankingCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.item];
    cell.songListLabel.numberOfLines = 0;
    [cell.songListLabel sizeToFit];
    cell.songListLabel.font = [UIFont systemFontOfSize:13];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    C_RankingListPlayerViewController *player = [[C_RankingListPlayerViewController alloc] init];
    player.model = self.dataArr[indexPath.item];
    [self.navigationController pushViewController:player animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
